package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsUserRoleInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsUserRoleInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.entity.UserRoleInfo;
import cc.rengu.igas.bsps.common.enums.BspsMchntTypeEnum;
import cc.rengu.igas.bsps.common.enums.BspsUserRoleTypeEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.MchntBindInfoBean;
import cc.rengu.igas.bsps.facade.bean.PrivilegeInfoBean;
import cc.rengu.igas.bsps.facade.request.MchntBindListQueryRequset;
import cc.rengu.igas.bsps.facade.response.MchntBindListQueryResponse;
import cc.rengu.igas.share.core.model.AgentMchntInfo;
import cc.rengu.igas.share.core.model.GroupMchntInfo;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.model.PlatMchntInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wangcheng on 2021/6/18 15:49
 */
public class MchntBindListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntBindListQueryRequset mchntBindListQueryRequset = new MchntBindListQueryRequset();
        ConvertUtil.convertOutput(mchntBindListQueryRequset);
        return mchntBindListQueryRequset;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntBindListQueryRequset mchntBindListQueryRequset = (MchntBindListQueryRequset) request;
        String instId = mchntBindListQueryRequset.getHeader().getInstId();
        String mchntNo = mchntBindListQueryRequset.getMchntNo();
        String userId = mchntBindListQueryRequset.getUserId();
        MchntBindListQueryResponse mchntBindListQueryResponse = new MchntBindListQueryResponse();
        mchntBindListQueryResponse.setHeader(mchntBindListQueryRequset.getHeader());
        /* 参数合法性检查 */
        if (StringUtil.isEmptyOrNull(instId) || (StringUtil.isEmptyOrNull(userId) && StringUtil.isEmptyOrNull(mchntNo))) {
            rglog.error("参数必填性校验失败！instId:<{}>,userId:<{}>,mchntNo:<{}>", instId, userId, mchntNo);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /* 获取用户绑定信息及对应角色权限 */
        BspsMchntBindInfoMapper bspsMchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
        BspsUserRoleInfoMapper bspsUserRoleInfoMapper = new BspsUserRoleInfoMapperImpl();
        List<MchntBindInfo> mchntBindInfoList;
        if (!StringUtil.isEmptyOrNull(mchntNo)) {
            mchntBindInfoList = bspsMchntBindInfoMapper.selectBspsMchntBindInfoListByMchntNoAndChainMchntNo(instId, userId, mchntNo);
        } else {
            mchntBindInfoList = bspsMchntBindInfoMapper.selectBspsMchntBindInfoListByUserId(instId, userId);
        }
        List<MchntBindInfoBean> facadeMchntBindInfoBeanList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(mchntBindInfoList)) {
            rglog.info("商户绑定关系赋值开始");
            for (MchntBindInfo mchntBindInfo : mchntBindInfoList) {
                MchntBindInfoBean facadeMchntBinInfo = new MchntBindInfoBean();
                BeanUtil.beanCopy(mchntBindInfo, facadeMchntBinInfo);
                facadeMchntBinInfo.setMchntType(mchntBindInfo.getBindMchntType());
                rglog.info("用户角色权限查询");
                List<UserRoleInfo> userRoleInfoList = bspsUserRoleInfoMapper.selectBspsUserRoleInfoToChannelMenu(instId, mchntBindInfo.getBindUserRole(), mchntBindListQueryRequset.getHeader().getChanlId());
                if (!CollectionUtils.isEmpty(userRoleInfoList)) {
                    List<PrivilegeInfoBean> privilegeInfoBeanList = userRoleInfoList.stream().map(this::convertPrivilegeInfo).collect(Collectors.toList());
                    facadeMchntBinInfo.setPrivilegeInfoBeanList(privilegeInfoBeanList);
                }
                rglog.info("商户类型赋值");
                facadeMchntBinInfo.setMchntType(mchntBindInfo.getBindMchntType());

                /*退款权限-法人、店长都有权限,白名单的收银员有权限*/
                if (((BspsUserRoleTypeEnum.MCHNT_ARTIF.getRoleType().equals(mchntBindInfo.getBindUserRole()))
                        || (BspsUserRoleTypeEnum.MCHNT_MANAGER.getRoleType().equals(mchntBindInfo.getBindUserRole())))) {
                    rglog.info("法人、店长登录");
                    //有退款权限
                    facadeMchntBinInfo.setRefundFlag(AppParamConstant.YES);
                } else {
                    rglog.info("非法人、店长登录");
                    //校验白名单收银员退款权限,null/NO为无权限
                    SysParamService sysParamService = new SysParamServiceImpl();
                    SysParam sysParam = sysParamService.getSysParamInfo(instId, BspsAppParamConstant.BSPS_REFUND_MERCH, mchntBindInfo.getMchntNo());
                    if (null != sysParam && !StringUtil.isEmptyOrNull(sysParam.getParamValue()) && AppParamConstant.YES.equals(sysParam.getParamValue())) {
                        rglog.info("白名单商户");
                        facadeMchntBinInfo.setRefundFlag(AppParamConstant.YES);
                    } else {
                        rglog.info("非白名单商户");
                        facadeMchntBinInfo.setRefundFlag(AppParamConstant.NO);
                    }
                }

                rglog.info("判断商户类型及状态");
                MchntService mchntService = new MchntServiceImpl();
                if (BspsMchntTypeEnum.AGENT_MCHNT.getMchntType().equals(mchntBindInfo.getBindMchntType())) {
                    AgentMchntInfo agentMchntInfo = mchntService.getAgentMchntInfo(instId, mchntBindInfo.getMchntNo());
                    if (null != agentMchntInfo) {
                        facadeMchntBinInfo.setMchntName(agentMchntInfo.getAgentMchntName());
                        facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                    }
                } else if (BspsMchntTypeEnum.PLAT_MCHNT.getMchntType().equals(mchntBindInfo.getBindMchntType())) {
                    PlatMchntInfo platMchntInfo = mchntService.getPlatMchntInfo(instId, mchntBindInfo.getMchntNo());
                    if (null != platMchntInfo) {
                        facadeMchntBinInfo.setMchntName(platMchntInfo.getPlatMchntName());
                        facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                    }
                } else if (BspsMchntTypeEnum.GROUP_MCHNT.getMchntType().equals(mchntBindInfo.getBindMchntType())) {
                    GroupMchntInfo groupMchntInfo = mchntService.getGroupMchntInfo(instId, mchntBindInfo.getMchntNo());
                    if (null != groupMchntInfo) {
                        facadeMchntBinInfo.setMchntName(groupMchntInfo.getGroupMchntName());
                        facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                    }
                } else {
                    MchntInfo mchntInfo = mchntService.getMchntInfo(instId, mchntBindInfo.getMchntNo());
                    if (null != mchntInfo) {
                        facadeMchntBinInfo.setMchntName(mchntInfo.getMchntName());
                        facadeMchntBinInfo.setCashWithdrawalFlag(mchntInfo.getCashWithdrawalFlag());
                        facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                    }
                }
            }
            mchntBindListQueryResponse.setMchntBindInfoBeanList(facadeMchntBindInfoBeanList);
        }
        /* 拼装响应结果 */
        BizResponse<MchntBindListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntBindListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntBindListQueryResponse mchntBindListQueryResponse = (MchntBindListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntBindListQueryResponse);
    }

    private PrivilegeInfoBean convertPrivilegeInfo(UserRoleInfo userRoleInfo) {
        PrivilegeInfoBean privilegeInfoBean = new PrivilegeInfoBean();
        BeanUtil.beanCopy(userRoleInfo, privilegeInfoBean);
        return privilegeInfoBean;
    }
}
